package com.macro.mall.config;

import cn.dev33.satoken.stp.StpUtil;
import com.macro.mall.common.constant.AuthConstant;
import com.macro.mall.common.dto.UserDto;
import com.macro.mall.operating.beans.Operator;
import com.macro.mall.operating.service.IOperatorGetService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

@Configuration
public class LogRecordConfiguration {

    @Bean
    public IOperatorGetService operatorGetService() {
        return () -> Optional.of(StpUtil.getSession().get(AuthConstant.STP_ADMIN_INFO))
                .map(a -> new Operator(((UserDto) a).getUsername()))
                .orElseThrow(() -> new IllegalArgumentException("user is null"));
    }
}

/*
//也可以这么搞：
@Service
public class DefaultOperatorGetServiceImpl implements IOperatorGetService {

    @Override
    public Operator getUser() {
        Operator operatorDO = new Operator();
        operatorDO.setOperatorId("SYSTEM");
        return operatorDO;
    }
}*/
